package com.yituo.mjworld.mvc.dao;

import com.yituo.mjworld.core.utils.BaseDao;
import com.yituo.mjworld.mvc.entity.web.*;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xq
 */
@Repository
public class UserDao extends BaseDao {

  /**
   * 是否已经实名认证
   *
   * @param userId
   * @return
   */
  public boolean isValidatePass(String userId) {
    String hql = "from UserInfoRecord where userId=:userId";
    List<UserInfoRecord> list = getSession().createQuery(hql).setParameter("userId", userId).list();
    if (list != null && list.size() > 0) {
      UserInfoRecord record = list.get(0);
      if (StringUtils.isEmpty(record.getAddress())
          || StringUtils.isEmpty(record.getBusinessLicense())
          || StringUtils.isEmpty(record.getIdCardImage1())
          || StringUtils.isEmpty(record.getIdCardImage2())
          || StringUtils.isEmpty(record.getMoblie())
          || StringUtils.isEmpty(record.getTelPhone())) {
        return false;
      }
      return true;
    }
    return false;
  }

  public WebUserQrcode getUserQrCodeInfo(String userId) {
    String hql = "from WebUserQrcode where userId=:userId";
    List<WebUserQrcode> list = getSession().createQuery(hql).setParameter("userId", userId).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }


  /**
   * 获取用户的收获地址.
   * 有一条记录时返回该条记录.
   * 有多条记录时,有默认地址返回默认地址.没有默认地址则返回第一条地址.
   * 没有收货地址时返回null
   *
   * @param userId 欲查询的用户ID
   * @return
   */
  public WebUserAddress getUserAddress(String userId) {
    String hql = "from WebUserAddress where userId=:userId";
    List<WebUserAddress> list = getSession().createQuery(hql).setParameter("userId", userId).list();
    if (list != null && list.size() > 0) {
      WebUserAddress address = list.get(0);
      for (WebUserAddress temp : list) {
        if (temp.getIsDefault()) {
          address = temp;
          break;
        }
      }
      return address;
    }
    return null;
  }

  /**
   * 获取用户的收获地址.
   * 有一条记录时返回该条记录.
   * 有多条记录时,有默认地址返回默认地址.没有默认地址则返回第一条地址.
   * 没有收货地址时返回null
   *
   * @param userId 欲查询的用户ID
   * @return
   */
  public WebUserAddress getUserAddress(int addressId, String userId) {
    String hql = "from WebUserAddress where userId=:userId and id=:addressId";
    List<WebUserAddress> list = getSession().createQuery(hql).setParameter("userId", userId).setParameter("addressId", addressId).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

  /**
   * 更新用户全部的地址为非默认
   *
   * @param userId
   * @return
   */
  public int updateUserAllAddress(String userId) {
    String sql = "update web_user_address set isDefault=false where userId=:userId";
    int i = getSession().createNativeQuery(sql).setParameter("userId", userId).executeUpdate();
    return i;
  }

  /**
   * 手机号码是否已经注册
   *
   * @param telPhone
   * @return
   */
  public WebUser getUserByMobile(String telPhone) {
    String hql = "from WebUser where telPhone=:telPhone";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("telPhone", telPhone);
    List<WebUser> list = find(hql, param);
    if (list != null && list.size() == 1) {
      return list.get(0);
    }
    return null;
  }

  /**
   * 登录查询
   *
   * @param account
   * @param password
   * @return
   */
  public WebUser login(String account, String password) {
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("account", account);
    param.put("password", password);
    String hql = "from WebUser where account=:account and password=:password";
    List<WebUser> list = find(hql, param);
    if (list != null && list.size() == 1) {
      return list.get(0);
    }
    return null;
  }

  /**
   * 获取用户通过帐号
   *
   * @param account
   * @return
   */
  public WebUser getUserByAccount(String account) {
    String hql = "from WebUser where account=:account";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("account", account);
    List<WebUser> list = find(hql, param);
    if (list != null && list.size() == 1) {
      return list.get(0);
    }
    return null;
  }

  /**
   * 判断帐号已经被占用
   *
   * @param account
   * @return
   */
  public boolean accountsIsExits(String account) {
    String hql = "from WebUser where account=:account";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("account", account);
    List<WebUser> list = find(hql, param);
    if (list != null && list.size() > 0) {
      return true;
    }
    return false;
  }

  /**
   * 获取用户的微信信息
   *
   * @param userId
   * @return
   */
  public WxUserinfo getWxUserInfoByUserId(String userId) {
    return (WxUserinfo) get(WxUserinfo.class, userId);
  }

  public WxUserinfo getWxUserInfoByOpenId(String openid) {
    String hql = "from WxUserinfo where openid=:openid";
    Map<String, Object> param = new HashMap<String, Object>(2);
    param.put("openid", openid);
    List<WxUserinfo> list = find(hql, param);
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

}
